package com.ridu.project.beshid.controller;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.entity.CodeEntity;
import com.ridu.project.beshid.repository.CodeRepository;
import com.ridu.project.beshid.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.UUID;

/**
 * 考试题目表
 * @author 随便 2020-05-17
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeRepository codeRepository;

    /**
     * 新增或编辑
     */
    @PostMapping("/save")
    public Object save(CodeEntity code){
        return codeRepository.save(code);
    }

    /**
     * 生成一批code
     */
    @PostMapping("/generateCode")
    public Object generateCode(Integer jifen, Integer num, Integer day, String reason){
        for(int i=0; i<num; i++){
            CodeEntity code = new CodeEntity();
            code.setCodeName(UUID.randomUUID().toString().replace("-", ""));
            code.setJifen(jifen);
            code.setReason(reason);
            code.setPassDay(day);
            code.setPassTime(DateUtil.addDay(new Date(), day));
            codeRepository.save(code);
        }
        return GeneralResponse.success("成功");
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public Object delete(long id){
        codeRepository.delete(codeRepository.findById(id).get());
        return GeneralResponse.success("删除成功");
    }

    /**
     * 查询
     */
    @PostMapping("/find")
    public Object find(long id){
        CodeEntity code=codeRepository.findById(id).get();
        return GeneralResponse.success(code);
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(CodeEntity code,
                       @RequestParam(required = false, defaultValue = "0") int currentPage,
                       @RequestParam(required = false, defaultValue = "10") int pageSize) {

        //创建实例
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(currentPage - 1, pageSize, sort);
        Page<CodeEntity> page = codeRepository.findAll(new Specification<CodeEntity>() {
            @Override
            public Predicate toPredicate(Root<CodeEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate isDelete = null;
                isDelete = cb.equal(root.<Boolean>get("deleted"), false);
                query.where(isDelete);
                return null;
            }
        }, pageable);
        GeneralResponse response = new GeneralResponse();
        response.setRows(page.getContent());
        response.setTotal(page.getTotalElements());
        return response;
    }

}
